package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *personrpc.UpdateRequest) (*personrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var findper mysql.Person
	mysql.DB.Find(&findper)
	if findper.ID == 0 {
		return nil, fmt.Errorf("该用户还未注册")
	}
	var per = mysql.Person{
		Model: gorm.Model{ID: uint(in.Id)},
		Name:  in.Name,
		Img:   in.Img,
		Tel:   in.Tel,
	}
	mysql.DB.Updates(&per)

	return &personrpc.UpdateResponse{Id: int64(per.ID)}, nil
}
